<?php
namespace api\user\controller;

use api\common\controller\RestUserBaseController;
use api\user\model\ConfigModel;
use api\user\model\NoticeModel;
use api\user\model\BannerModel;
use think\Db;

class IndexController extends RestUserBaseController
{
    protected $userId;

    public function __construct()
    {
        parent::__construct();
        $this->userId = $this->getUserId();
    }


    public function index()
    {
        $map['page'] = '1';   
        $map['order'] = '-createtime';
        if(request()->header('lang') == 'en'){
            $map['where']['sign'] = 1;
        }else{
            $map['where']['sign'] = 0;
        }
        $NoticeModel = new NoticeModel();
        $BannerModel = new BannerModel();
        $banner = $BannerModel->select();
        $notice = $NoticeModel->getDatas($map);
        foreach ($notice as &$v){
            $preUrl = request()->domain();
            $v['notice_content'] = preg_replace('#src="/([^"]+?)"#','src="'.$preUrl.'/$1"',$v['notice_content']);
        }
        //24小币币交易时成交总量
        $total_mu = Db::name('user_order')
        ->where(['order_type'=>1,'order_status'=>1])
        ->whereTime('createtime','-24 hours')
        ->order('sell_price desc')
        ->sum('coin_num');

        //最近币币交易成交最高价2条
        $newmu = Db::name('user_order')
        ->where(['order_type'=>1,'order_status'=>1])
        ->whereTime('createtime','-24 hours')
        ->order('sell_price desc')
        ->limit(2)
        ->select();
        $mupre = isset($newmu[1]['sell_price'])?$newmu[1]['sell_price']:0;
        $munext = isset($newmu[0]['sell_price'])?$newmu[0]['sell_price']:0;
        $murange = 0;
        if(($mupre>0)&&($munext>0)){
            $murange = 1-($mupre/$munext);
        }

        //24小OTC交易时成交总量
        $total_mq = Db::name('user_order')
        ->where(['order_type'=>3,'order_status'=>1])
        ->whereTime('createtime','-24 hours')
        ->order('sell_price desc')
        ->sum('coin_num');
        //最近OTC交易成交最高价2条
        $newmq = Db::name('user_order')
        ->where(['order_type'=>3,'order_status'=>1])
        ->whereTime('createtime','-24 hours')
        ->order('sell_price desc')
        ->limit(2)
        ->select();

        $mqpre = isset($newmq[1]['sell_price'])?$newmq[1]['sell_price']:0;
        $mqnext = isset($newmq[0]['sell_price'])?$newmq[0]['sell_price']:0;
        $mqrange = 0;
        if(($mqpre>0)&&($mqnext>0)){
            $mqrange = 1-($mqpre/$mqnext);
        }
        $mq_to_mu = ConfigModel::getConfig('trade','mq_to_mu');
        $mq_to_mu_price = isset($mq_to_mu['value']) ? $mq_to_mu['value'] : 100;

        $mq_range = ConfigModel::getConfig('trade','mq_range');
        $mq_range_price = isset($mq_range['value']) ? $mq_range['value'] : 100;
        $mu_range = ConfigModel::getConfig('trade','mu_range');
        $mu_range_price = isset($mu_range['value']) ? $mu_range['value'] : 100;

        $mqnext = $mq_to_mu_price;

        $remmond['mu']['total'] = $total_mu;
        $remmond['mu']['new']   = 1;
        $remmond['mu']['range'] = $mu_range_price;
        $remmond['mq']['total'] = $total_mq;
        $remmond['mq']['new']   = $mqnext;
        $remmond['mq']['range'] = $mqrange;
        $this->success('请求成功', [
            'notice' =>$notice,
            'banner'=>$banner,
            'remmond'=>$remmond
        ]);
    }

    public function getOneNotice(){
        $type = $this->request->get('type');
        $NoticeModel = new NoticeModel();
        $map['type'] = $type;
        if(request()->header('lang') == 'en'){
            $map['sign'] = 1;
        }else{
            $map['sign'] = 0;
        }
        $data = $NoticeModel->where($map)->order('createtime desc')->find();
        if(empty($data)){
            $this->success('无数据');
        }
        $preUrl = request()->domain();
        $data['notice_content'] = preg_replace('#src="/([^"]+?)"#','src="'.$preUrl.'/$1"',$data['notice_content']);
        $this->success('请求成功',$data);
    }

}
